package com.example.chatchatbackend.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.chatchatbackend.entity.dto.UserTags;
import com.example.chatchatbackend.entity.vo.request.UserTagsVO;
import com.example.chatchatbackend.mapper.UserTagsMapper;
import com.example.chatchatbackend.service.UserTagsService;
import com.example.chatchatbackend.utils.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Service
public class UserTagsServiceImpl extends ServiceImpl<UserTagsMapper, UserTags> implements UserTagsService {

    private static final LocalDateTime currentTime  = TimeUtil.Time_Now.getCurrentTime();

    @Autowired
    private UserTagsMapper userTagsMapper;

    @Override
    public List<UserTagsVO> getAllUserTags() {
        List<UserTags> userTagsList = this.query().list();
        List<UserTagsVO> userTagsVOList = new ArrayList<>();
        for(UserTags userTags : userTagsList){
            userTagsVOList.add(new UserTagsVO(userTags.getId(),userTags.getTag(),userTags.getCreatedTime(),userTags.getUpdateTime()));
        }
        return userTagsVOList;
    }

    @Override
    public UserTagsVO insertUserTags(UserTagsVO userTagsVO) {
        UserTags comment = new UserTags(null,userTagsVO.getTag(),this.currentTime,this.currentTime);
        if(this.save(comment))
            return userTagsVO;
        return null;
    }

    @Override
    public List<UserTagsVO> insertUserTagsList(List<UserTagsVO> userTagsVOList) {

        List<UserTags> userTagsList = new ArrayList<>();

        for (UserTagsVO userTagsVO : userTagsVOList) {
            userTagsList.add(new UserTags(null,userTagsVO.getTag(),currentTime,this.currentTime));
        }
        userTagsMapper.insertList(userTagsList);
        return userTagsVOList;
    }

    @Override
    public IPage<UserTags> pageThree(Integer pageNum, Integer pageSize) {
        Page<UserTags> page = new Page<>(pageNum, pageSize);
        IPage<UserTags> userTagsIPage = userTagsMapper.selectPage(page, null);
        return userTagsIPage;
    }

    @Override
    public UserTagsVO updateUserTags(UserTagsVO userTagsVO) {
        UserTags userTags = new UserTags();
        BeanUtils.copyProperties(userTagsVO, userTags);
        userTags.setUpdateTime(this.currentTime);
        userTagsMapper.updateUserTags(userTags);
        return userTagsVO;
    }


    @Override
    public void deleteByIds(List<Integer> ids) {
        userTagsMapper.deleteBatchIds(ids);
    }

    public List<UserTagsVO> findByIds(List<Integer> ids) {
        List<UserTags> userTagsList = userTagsMapper.selectBatchIds(ids);
        List<UserTagsVO> userTagsVOList = new ArrayList<>();

        for (UserTags userTags : userTagsList) {
            UserTagsVO userTagsVO = new UserTagsVO();
            BeanUtils.copyProperties(userTags, userTagsVO);
            userTagsVOList.add(userTagsVO);  // 添加到结果列表
        }

        return userTagsVOList;
    }

}
